package com.indusfo.spc.service.impl;

import com.indusfo.spc.mapper.CodeGroupMapper;
import com.indusfo.spc.mapper.RuleListMapper;
import com.indusfo.spc.pojo.CodeGroup;
import com.indusfo.spc.pojo.RuleList;
import com.indusfo.spc.service.BarCodeRules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class BarCodeRulesImpl implements BarCodeRules {

    @Resource
    private RuleListMapper ruleListMapper;

    @Resource
    private CodeGroupMapper groupMapper;

    @Override
    public List<String> calculate(Integer ruleId , Integer num) {
        List<String> strings = new ArrayList<>();
        RuleList ruleList = new RuleList();
        ruleList.setRuleId(ruleId);
        List<RuleList> ruleLists = ruleListMapper.listRules(ruleList);
        String constant = "";// 定值
        String variable = "";// 变量
        String stepize = "";// 步长
        for (RuleList rulelist : ruleLists) {
            if (rulelist.getCl() == 1) {
                constant = constant + rulelist.getFirst();
            } else {
                if (rulelist.getTypeId() == 2) {
                    String date = "";
                    SimpleDateFormat df = null;
                    if (rulelist.getFormId() == 1) {
                        df = new SimpleDateFormat("YYYY");//设置日期格式
                    }
                    if (rulelist.getFormId() == 2) {
                        df = new SimpleDateFormat("YY");//设置日期格式
                    }
                    if (rulelist.getFormId() == 3) {
                        df = new SimpleDateFormat("MM");//设置日期格式
                    }
                    if (rulelist.getFormId() == 4) {
                        df = new SimpleDateFormat("dd");//设置日期格式
                    }
                    date = df.format(new Date());
                    constant = constant + date;
                }
                if (rulelist.getTypeId() == 4) {
                    String first = rulelist.getFirst();
                    constant = constant + first;
                }
                if (rulelist.getTypeId() == 1) {
                    String first = rulelist.getFirst();
                    String len = rulelist.getLen();
                    stepize = rulelist.getLength();
                    variable = variable + String.format("%0" + len + "d", Integer.valueOf(first));
                }
            }
        }
        CodeGroup codeGroup = new CodeGroup();
        codeGroup.setRuleId(ruleId);
        codeGroup.setStaticData(constant);
        CodeGroup codeGroup1 = groupMapper.selectCodeGroup(codeGroup);
        if(codeGroup1 != null){
            variable = codeGroup1.getLastData();
            strings=List(num,stepize,variable,ruleLists);
            codeGroup1.setLastData( String.format("%0" + variable.length() + "d", Integer.valueOf(variable) +num*Integer.valueOf(stepize)));
            groupMapper.updateByPrimaryKeySelective(codeGroup1);
            return strings;
        }
        System.out.println(constant);
        System.out.println(variable);
        System.out.println(stepize);
        strings=List(num,stepize,variable,ruleLists);
        codeGroup.setLastData( String.format("%0" + variable.length() + "d", Integer.valueOf(variable) +num*Integer.valueOf(stepize)));
        groupMapper.insertSelective(codeGroup);
        return strings;
    }

    public List<String> List(Integer num ,String stepize ,String variable ,List<RuleList> ruleLists){
        List<String> stringList = new ArrayList<>();
        String variable1 = "";
        for(int i = 0 ;i < num * Integer.valueOf(stepize); i = i + Integer.valueOf(stepize)){
            String s = "";
            for (RuleList rulelist: ruleLists) {
                if(rulelist.getCl()==1){
                    s = s + rulelist.getFirst();
                } else{
                    if(rulelist.getTypeId()==2){
                        String  date = "";
                        SimpleDateFormat df = null;
                        if(rulelist.getFormId()==1){
                            df = new SimpleDateFormat("YYYY");//设置日期格式
                        }
                        if(rulelist.getFormId()==2){
                            df = new SimpleDateFormat("YY");//设置日期格式
                        }
                        if(rulelist.getFormId()==3){
                            df = new SimpleDateFormat("MM");//设置日期格式
                        }
                        if(rulelist.getFormId()==4){
                            df = new SimpleDateFormat("dd");//设置日期格式
                        }
                        date = df.format(new Date());
                        s = s + date;
                    }
                    if(rulelist.getTypeId()==4){
                        String first = rulelist.getFirst();
                        s = s + first;
                    }
                    if(rulelist.getTypeId()==1){
                        String len = rulelist.getLen();
                        s = s + String.format("%0"+len+"d", Integer.valueOf(variable) + i);
                        variable1 = String.format("%0"+len+"d", Integer.valueOf(variable) + i);
                    }
                }
            }
            stringList.add(s);
        }
        System.out.println(variable1);
        return stringList;
    }


}
